﻿using DragonBones;

using System;
using System.Collections.Generic;

using UnityEngine;

namespace Assets.XyzCode.Fight.Game.Player
{
    /// <summary>
    /// 死亡小姐行为
    /// </summary>
    public partial class Player_MissDie_Behav : MonoBehaviour
    {
        /** 视图, 也就是龙骨动画节点 */
        private GameObject _VIEW;

        /** 可攻击目标字典 */
        private readonly IDictionary<int, WeakReference<GameObject>> _attkableTargetDict = new Dictionary<int, WeakReference<GameObject>>();

        /// <summary>
        /// Start
        /// </summary>
        private void Start()
        {
            _VIEW = transform.Find("VIEW").gameObject;
            _VIEW.GetComponent<UnityArmatureComponent>().AddEventListener(EventObject.FRAME_EVENT, DBEventHandler);
        }

        /// <summary>
        /// Update
        /// </summary>
        private void Update()
        {
            // 更新当前状态
            UpdateCurrState();
        }

        /// <summary>
        /// LateUpdate
        /// </summary>
        private void LateUpdate()
        {
            _VIEW.transform.rotation = Camera.main.transform.rotation;
        }

        /// <summary>
        /// 当发生碰撞
        /// </summary>
        /// <param name="c">碰撞目标</param>
        private void OnCollisionEnter(Collision c)
        {
            if (null == c)
            {
                return;
            }

            int goInstanceId = c.gameObject.GetInstanceID();

            // XXX 注意: 添加碰撞盒后, 如果角色转身, 
            // 也就是面向左方时, 
            // Unity 引擎会抛出一个警告:
            // "BoxColliders does not support negative scale or size."
            // 究其原因是在转身的时候用的是缩放 X 轴的策略...
            // 缩放 X 轴的策略还是 2D 游戏的思路!
            // 因为缺少第三个维度, 所以只能这样做...
            // 但是到了 3D 场景中, 
            // 我们可以通过以 Y 轴来旋转解决这个问题!
            // 
            if (!_attkableTargetDict.ContainsKey(goInstanceId))
            {
                _attkableTargetDict[goInstanceId] = new WeakReference<GameObject>(c.gameObject);
            }
        }

        /// <summary>
        /// 当离开碰撞
        /// </summary>
        /// <param name="c">碰撞目标</param>
        private void OnCollisionExit(Collision c)
        {
            if (null == c)
            {
                return;
            }

            int goInstanceId = c.gameObject.GetInstanceID();

            if (_attkableTargetDict.ContainsKey(goInstanceId))
            {
                _attkableTargetDict.Remove(goInstanceId);
            }
        }

        /// <summary>
        /// 播放龙骨 ( DragonBonez ) 动画
        /// </summary>
        /// <param name="animName">动画名称</param>
        /// <param name="playTimez">播放次数, -1 不限次数</param>
        /// <param name="timeScale">时间比例</param>
        private void PlayDBAnim(string animName, int playTimez = -1, float timeScale = 1)
        {
            if (null == _VIEW)
            {
                return;
            }

            // 获取龙骨动画组件
            UnityArmatureComponent dbComp = _VIEW.GetComponent<UnityArmatureComponent>();

            if (null != dbComp)
            {
                dbComp.animation.timeScale = timeScale;
                dbComp.animation.Play(animName, playTimez);
            }
        }

        /// <summary>
        /// 龙骨事件句柄
        /// </summary>
        /// <param name="type">事件类型</param>
        /// <param name="eventObj">事件对象</param>
        private void DBEventHandler(string type, EventObject eventObj)
        {
            if (null == eventObj
             || string.IsNullOrEmpty(eventObj.name))
            {
                return;
            }

            if (eventObj.name.StartsWith("Punch") 
             && eventObj.name.EndsWith("Hit"))
            {
                OnPunch_X_Hit(eventObj.name);
            }
        }

        /// <summary>
        /// 是否面向右
        /// </summary>
        /// <returns>true = 面向右, false = 面向左</returns>
        private bool IsFaceToRight()
        {
            return Mathf.Abs(FACE_TO_R.eulerAngles.y - transform.localRotation.eulerAngles.y) <= 1f;
        }
    }
}
